﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddName : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidmodie.Value = Request.QueryString["moduid"].ToString();
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            if (Request.QueryString["action"].ToString() == "update")
            {
                hidId.Value = Request.QueryString["ComoditiesNameId"].ToString();
                initName();
               
                btnSave.Text = " 修改 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
            if (Request.QueryString["action"].ToString() == "add")
            {
          
                btnSave.Text = " 添加 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                initType();
            }
            if (Request.QueryString["action"].ToString() == "flag")
            {
                hidId.Value = Request.QueryString["ComoditiesNameId"].ToString();
                initName();
         
                btnSave.Text = " 审核 ";
            }
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
         try
        {
        ZSql zs = new ZSql();

        if (!string.IsNullOrEmpty(hidId.Value))
        {
         
            int flag;
            if (rbtnFlag.Checked)
            {
                flag = 1;
            }
            else
            {
                flag = 0;
            }

            if (Request.QueryString["action"].ToString() == "flag")
            {
                zs.Open("update ComoditiesName set UserName='" + lblUser.Text + "',usTime='" + lblTime.Text + "',AuditState='" + flag + "',namebh='"+Common.FormatParameter(txtbh.Text)+"' where ComoditiesNameId=" + hidId.Value);
                Response.Write("<script>window.location='NameManage.aspx?moduid=" + Request["moduid"] + "';</script>");
            }
            else
            {
                //zs.Open("select * from ComoditiesName where TypeId=" + ddlType.SelectedValue + "and BrandId=" + ddlBrand.SelectedValue + " and ComoditiesName='" + Common.FormatParameter(txtName.Text) + "'");
                //if (zs.m_table.Rows.Count > 0)
                //{
                //    Response.Write("<script>alert('该商品名已存在');location.href=document.referrer</script>");
                //    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                //    return;
                //}
                zs.Open("update ComoditiesName set ComoditiesName='" + Common.FormatParameter(txtName.Text) + "',TypeId=" + ddlType.SelectedValue + ",BrandId=" + ddlBrand.SelectedValue + ",ComoditiesNameOrder='" + Common.FormatParameter(nameOrder.Text) + "',UserName='" + lblUser.Text + "',usTime='" + lblTime.Text + "',namebh='"+Common.FormatParameter(txtbh.Text)+"' where ComoditiesNameId=" + hidId.Value);
               
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                Response.Write("<script>window.location='NameManage.aspx?moduid=" + Request["moduid"] + "';</script>");
            }
        }
        else
        {
            if (ddlType.SelectedValue == "0")
            {
                Response.Write("<script>alert('请选择商品类型');location.href=document.referrer</script>");
            }
            if (ddlBrand.SelectedValue == "0")
            {
                Response.Write("<script>alert('请选择商品品牌');location.href=document.referrer</script>");
            }

            zs.Open("select * from ComoditiesName where TypeId=" + ddlType.SelectedValue + "and BrandId="+ddlBrand.SelectedValue+" and ComoditiesName='" + txtName.Text + "'");
            if (zs.m_table.Rows.Count > 0)
            {
                Response.Write("<script>alert('该商品名已存在');location.href=document.referrer</script>");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                return;
            }
            zs.Open("insert into ComoditiesName (ComoditiesName,TypeId,BrandId,ComoditiesNameOrder,UserName,usTime,namebh) values('" + Common.FormatParameter(txtName.Text) + "'," + ddlType.SelectedValue + "," + ddlBrand.SelectedValue + ",'" + Common.FormatParameter(nameOrder.Text) + "','" + lblUser.Text + "','" + lblTime.Text + "','"+Common.FormatParameter(txtbh.Text)+"')");

         
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            Response.Write("<script>window.location='NameManage.aspx?moduid=" + Request["moduid"] + "';</script>");
        }
        zs.Close();
    }
    catch (Exception)
    {

        Response.Write("<script>alert('添加失败');location.href=document.referrer</script>");
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
    }
}

    private void initType()
    {
      
        ZSql type = new ZSql();
        type.Open("select * from ComoditiesType ");
        if (type.m_table.Rows.Count > 0)
        {
            type.Open("select * from ComoditiesType where AuditState=1");
            if (type.m_table.Rows.Count > 0)
            {
                ddlType.DataSource = type.m_table;
                ddlType.DataTextField = "ComoditiesType";
                ddlType.DataValueField = "TypeId";
                ddlType.DataBind(); 
                type.Close();
                ddlType.Items.Insert(0, new ListItem("—选择商品类型—", "0"));
                ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—", "0"));
            }
            else
            {
                Response.Write("<script>alert('类型未审核');</script>");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
        }
        else
        {
            Response.Write("<script>alert('类型为空请添加类型');</script>");
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }
       

        
    }

    protected void Linkcannel_Click(object sender, EventArgs e)
    {
        Response.Redirect("NameManage.aspx?moduid=" + hidmodie.Value);
    }

    private void initName()
    {
        if (!string.IsNullOrEmpty(hidId.Value))
        {
            ZSql zs = new ZSql();
            zs.Open("select * from ComoditiesName where ComoditiesNameId=" + hidId.Value.Trim());
            txtName.Text = zs.m_table.Rows[0]["ComoditiesName"].ToString();
            nameOrder.Text = zs.m_table.Rows[0]["ComoditiesNameOrder"].ToString();
            hidId1.Value = zs.m_table.Rows[0]["TypeId"].ToString();
            hidId2.Value = zs.m_table.Rows[0]["BrandId"].ToString();
            ZSql t = new ZSql();
            t.Open("select * from ComoditiesType where AuditState=1");
            ddlType.DataSource = t.m_table;
            ddlType.DataTextField = "ComoditiesType";
            ddlType.DataValueField = "TypeId";
            ddlType.DataBind();
            t.Close();
            ddlType.Items.Insert(0, new ListItem("—选择商品类型—","0"));
            ddlType.SelectedValue = zs.m_table.Rows[0]["TypeId"].ToString();
            ZSql p = new ZSql();
            p.Open("select * from Brand where BrandId=" + hidId2.Value + "and AuditState=1");
            ddlBrand.DataSource = p.m_table;
            ddlBrand.DataTextField = "Brand";
            ddlBrand.DataValueField = "BrandId";
            ddlBrand.DataBind();
            p.Close();
            ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—","0"));
            ddlBrand.SelectedValue = zs.m_table.Rows[0]["BrandId"].ToString();
            if (zs.m_table.Rows[0]["AuditState"].ToString() == "1")
            {
                rbtnFlag.Checked = true;
                rbtnnotFlag.Checked = false;
            }
            if (zs.m_table.Rows[0]["AuditState"].ToString() == "0")
            {
                rbtnFlag.Checked = false;
                rbtnnotFlag.Checked = true;
            }
            txtbh.Text = zs.m_table.Rows[0]["namebh"].ToString();
        }
    }
   
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlType.SelectedIndex > 0)
        {
            ZSql brand = new ZSql();
            brand.Open("select * from Brand where TypeId=" + ddlType.SelectedValue + "");
            if (brand.m_table.Rows.Count > 0)
            {
                brand.Open("select * from Brand where TypeId=" + ddlType.SelectedValue + " and AuditState=1");
                if (brand.m_table.Rows.Count > 0)
                {
                    ddlBrand.DataSource = brand.m_table;
                    ddlBrand.DataTextField = "Brand";
                    ddlBrand.DataValueField = "BrandId";
                    ddlBrand.DataBind();
                    ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—", "0"));
                    brand.Close();
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                }
                else
                {
                    Response.Write("<script>alert('该类品牌未审核');</script>");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                }
            }
            else
            {
                Response.Write("<script>alert('该类品牌为空请添加品牌');</script>");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—", "0"));
            }
        }
        else
        {
            ddlBrand.Items.Clear();
            ddlBrand.Items.Insert(0, new ListItem("—选择商品品牌—","0"));
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }
    }
}
